package net.mcg_server.jbi.gui.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import net.mcg_server.jbi.Program;
import net.mcg_server.jbi.gui.AddGame;
import net.mcg_server.jbi.gui.EditGame;
import net.mcg_server.jbi.xml.XMLReader;
import net.mcg_server.jbi.xml.XMLWriter;

@SuppressWarnings("serial")
public class MenuBar extends JMenuBar {

	private JFrame frame;
	private JMenu fileMenu;
	private JMenu editMenu;
	private JMenuItem newGameOrder;
	private JMenuItem saveGameOrder;
	private JMenuItem loadGameOrder;
	private JMenuItem closeApp;
	private JMenuItem addGame;
	private JMenuItem editGame;
	//private JMenuItem manageGames;
	public static boolean menuOpened = false;

	public MenuBar(JFrame pFrame, final Program program) {
		this.frame = pFrame;

		// add to the frame
		frame.setJMenuBar(this);

		// add menus
		fileMenu = new JMenu("File");
		editMenu = new JMenu("Edit");
		this.add(fileMenu);
		this.add(editMenu);

		// add File Menu options
		newGameOrder = new JMenuItem("New Game Order");
		saveGameOrder = new JMenuItem("Save Current Order");
		loadGameOrder = new JMenuItem("Load Game Order");
		closeApp = new JMenuItem("Exit");
		fileMenu.add(newGameOrder);
		fileMenu.add(saveGameOrder);
		fileMenu.add(loadGameOrder);
		fileMenu.add(new JSeparator());
		fileMenu.add(closeApp);

		// add Edit Menu options
		addGame = new JMenuItem("Add Game");
		editGame = new JMenuItem("Edit Game");
		//manageGames = new JMenuItem("Manage Games");
		editMenu.add(addGame);
		editMenu.add(editGame);
		//editMenu.add(manageGames);

		// --------------
		// LISTENERS
		// --------------

		// close the application
		closeApp.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				program.saveProperties();
				System.exit(0);
			}
		});

		// open the AddGame window
		addGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!menuOpened) {
					menuOpened = true;
					new AddGame(program);
				}
			}
		});

		// open the EditGame window
		editGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!menuOpened) {
					menuOpened = true;
					new EditGame(program);
				}
			}
		});

		// open the ManageGame window -- DISABLED --
		/*
		manageGames.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (!menuOpened) {
					menuOpened = true;
					new ManageGames(program);
				}
			}
		});
		*/
		
		// Initialize the SAVE functionality
		saveGameOrder.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				XMLWriter write = new XMLWriter();
				String loc = write.writeXml(program);
				
				program.writeLastFileProperty(loc);
				program.saveProperties();
			}
		});
		
		// Initialize the LOAD functionality
		loadGameOrder.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				XMLReader read = new XMLReader();
				read.readXml(program);
			}
		});
		
		// Initialize the NEW functionality
		newGameOrder.addActionListener(new ActionListener(){
			@Override 
			public void actionPerformed(ActionEvent e){
				program.clearGameList();
			}
		});

	}

}
